$(function () {

    $("body").on('click','.select-course-box',function () {//选择课程
        if($(this).hasClass('dc-active')){
            $(this).removeClass('dc-active');
        }else{
            $(this).addClass('dc-active');
        }
    }).on('click','#ensure-active-pack',function () {//执行激活
        activePack();//
    });

    /**
     * 执行激活
     */
    function activePack(){
        var list = $(".order-course-list>.dc-active");
        if(list==null || list.length <=0){
            $.toast('请选择要激活的课程~~');
            return;
        }
        var courseIds = list.map(function () {
            return $(this).data('course');
        }).get().join(",");
        var recordId = $("#recordId").val();
        var courseId = $("#courseId").val();
        $.ajax({
            url:baseloaction +'/mobile/uc/course/activationPackage',
            type:'post',
            dataType:'json',
            data:{
                'recordId':recordId,
                'courseIds':courseIds
            },
            success:function (result) {
                $.toast(result.message);
                if(result.success==true){
                    setTimeout(function () {
                        window.location.href = baseloaction +'/mobile/course/info?onLimit=true&courseId='+courseId;
                    },1500);
                }
            },
            error:function (error) {
                $.toast('系统错误，课程激活失败');
            }
        });
    }

    //执行获取数据
    getPackageCourseList();

    /**
     * 加载套餐下的课程数据
     */
    function getPackageCourseList() {
        var recordId = $("#recordId").val();
        $.ajax({
            url: baseloaction +'/mobile/uc/course/getActivationPackage',
            type:'post',
            dataType:'json',
            data:{'recordId':recordId},
            success:function (result) {
                if(result.success==true){
                    initData(result.entity);
                }else{
                    $.toast(result.message);
                }
            },
            error:function (error) {
                $.toast('系统错误，获取激活课程数据失败');
            }
        });
   }

    /**
     * 初始化页面数据
     * @param data 数据
     */
   function initData(data) {
       $("#courseName").text('[套餐]'+data.course.courseName);
       var packageCourseList = data.course.packageCourseList;
       if(packageCourseList!=null && packageCourseList.length > 0){
           var context = '';
           $.each(packageCourseList,function (index,em) {
               var active = '';
               var className = '';
               if(em.required == 1){//必选
                   active ='dc-active';
               }
               if(em.required !=1){
                   className = 'select-course-box';
               }
               context += '<li class="item-content '+active+' '+className+'" data-course="'+em.course.id+'">';
               context += '     <div class="item-inner">';
               context += '         <div class="item-title">';
               context += '             <span class="fs7 c-666">'+em.course.courseName+'</span>';
               context += '         </div>';
               context += '         <div class="item-after">';
               context += '             <span class="dc-icon-bx ok-icon-bx dc-small-icon-bx">';
               context += '                 <em class="iconfont icon-duihao"></em>';
               context += '             </span>';
               context += '         </div>';
               context += '     </div>';
               context += '</li>';
           });
           $(".order-course-list").html(context);
       }
    }
});